{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sender With Largest Word Count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: largestWordCount"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #最多单词数的发件人"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个聊天记录，共包含 <code>n</code>&nbsp;条信息。给你两个字符串数组&nbsp;<code>messages</code> 和&nbsp;<code>senders</code>&nbsp;，其中&nbsp;<code>messages[i]</code>&nbsp;是&nbsp;<code>senders[i]</code>&nbsp;发出的一条&nbsp;<strong>信息</strong>&nbsp;。</p>\n",
    "\n",
    "<p>一条 <strong>信息</strong>&nbsp;是若干用单个空格连接的 <strong>单词</strong>&nbsp;，信息开头和结尾不会有多余空格。发件人的 <strong>单词计数</strong>&nbsp;是这个发件人总共发出的 <strong>单词数</strong>&nbsp;。注意，一个发件人可能会发出多于一条信息。</p>\n",
    "\n",
    "<p>请你返回发出单词数 <strong>最多</strong>&nbsp;的发件人名字。如果有多个发件人发出最多单词数，请你返回 <strong>字典序</strong>&nbsp;最大的名字。</p>\n",
    "\n",
    "<p><strong>注意：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>字典序里，大写字母小于小写字母。</li>\n",
    "\t<li><code>\"Alice\"</code> 和&nbsp;<code>\"alice\"</code>&nbsp;是不同的名字。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>messages = [\"Hello userTwooo\",\"Hi userThree\",\"Wonderful day Alice\",\"Nice day userThree\"], senders = [\"Alice\",\"userTwo\",\"userThree\",\"Alice\"]\n",
    "<b>输出：</b>\"Alice\"\n",
    "<b>解释：</b>Alice 总共发出了 2 + 3 = 5 个单词。\n",
    "userTwo 发出了 2 个单词。\n",
    "userThree 发出了 3 个单词。\n",
    "由于 Alice 发出单词数最多，所以我们返回 \"Alice\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>messages = [\"How is leetcode for everyone\",\"Leetcode is useful for practice\"], senders = [\"Bob\",\"Charlie\"]\n",
    "<b>输出：</b>\"Charlie\"\n",
    "<b>解释：</b>Bob 总共发出了 5 个单词。\n",
    "Charlie 总共发出了 5 个单词。\n",
    "由于最多单词数打平，返回字典序最大的名字，也就是 Charlie 。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == messages.length == senders.length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>1 &lt;= messages[i].length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= senders[i].length &lt;= 10</code></li>\n",
    "\t<li><code>messages[i]</code>&nbsp;包含大写字母、小写字母和&nbsp;<code>' '</code>&nbsp;。</li>\n",
    "\t<li><code>messages[i]</code>&nbsp;中所有单词都由 <strong>单个空格</strong>&nbsp;隔开。</li>\n",
    "\t<li><code>messages[i]</code>&nbsp;不包含前导和后缀空格。</li>\n",
    "\t<li><code>senders[i]</code>&nbsp;只包含大写英文字母和小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sender-with-largest-word-count](https://leetcode.cn/problems/sender-with-largest-word-count/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sender-with-largest-word-count](https://leetcode.cn/problems/sender-with-largest-word-count/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"Hello userTwooo\",\"Hi userThree\",\"Wonderful day Alice\",\"Nice day userThree\"]\\n[\"Alice\",\"userTwo\",\"userThree\",\"Alice\"]', '[\"How is leetcode for everyone\",\"Leetcode is useful for practice\"]\\n[\"Bob\",\"Charlie\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "      data = {}\n",
    "      for i in range(len(messages)):\n",
    "        if senders[i] not in data.keys():\n",
    "          data[senders[i]] = len(messages[i].split(' '))\n",
    "        else:\n",
    "          data[senders[i]] += len(messages[i].split(' '))\n",
    "\n",
    "      max_value = max(data.values())\n",
    "      res = [k for k,v in data.items() if v==max_value]\n",
    "      return max(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        nameDic = {\n",
    "        }\n",
    "        for n in range(0,len(messages)):\n",
    "            if senders[n] in nameDic:\n",
    "                nameDic[senders[n]] += len(messages[n].split(' '))\n",
    "            else:\n",
    "                nameDic[senders[n]] = len(messages[n].split(' '))\n",
    "       \n",
    "        name = max(zip(nameDic.values(), nameDic.keys()))\n",
    "        return name[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        c = Counter()\n",
    "        for m, s in zip(messages, senders):\n",
    "            c[s] += m.count(' ') + 1\n",
    "        return max(c, key=lambda s:(c[s], s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        d = Counter()\n",
    "        for m, s in zip(messages, senders):\n",
    "            d[s] += len(m.split())\n",
    "        ans = ''\n",
    "        mx = 0\n",
    "        for k, v in d.items():\n",
    "            if v > mx:\n",
    "                ans = k\n",
    "                mx = v\n",
    "            elif v == mx:\n",
    "                ans = max(ans, k)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution(object):\n",
    "    def largestWordCount(self, messages, senders):\n",
    "        \"\"\"\n",
    "        :type messages: List[str]\n",
    "        :type senders: List[str]\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        dict_ = {}\n",
    "        maxcount, maxsender = 0, \"\"\n",
    "        for i, sender in enumerate(senders):\n",
    "            if sender in dict_:\n",
    "                dict_[sender] += messages[i].count(' ') + 1\n",
    "            else:\n",
    "                dict_[sender] = messages[i].count(' ') + 1\n",
    "            if dict_[sender] > maxcount:\n",
    "                maxcount, maxsender = dict_[sender], sender\n",
    "            elif dict_[sender] == maxcount:\n",
    "                maxsender = max(maxsender, sender)\n",
    "        return maxsender\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        cnt = Counter()\n",
    "        for m, s in zip(messages, senders):\n",
    "            cnt.update({s: len(m.split())})\n",
    "        return max(cnt, key=lambda i: (cnt[i], i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        发送单词最多的发件人 = \"\"\n",
    "        最多单词数 = 0\n",
    "        字典 = dict()\n",
    "        for i in range(len(messages)):\n",
    "            字典[senders[i]] = 字典.get(senders[i], 0) + messages[i].count(\" \") + 1\n",
    "        for i,j in 字典.items():\n",
    "            if j > 最多单词数:\n",
    "                最多单词数 = j\n",
    "                发送单词最多的发件人 = i\n",
    "            if j == 最多单词数:\n",
    "                发送单词最多的发件人 = max(i, 发送单词最多的发件人)\n",
    "        return 发送单词最多的发件人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        m = defaultdict(int)\n",
    "        for message, sender in zip(messages, senders):\n",
    "            v = len(message.split(' '))\n",
    "            m[sender] += v\n",
    "        \n",
    "        maxval = max(m.values())\n",
    "        return sorted([k for k in m if m[k] == maxval], reverse=True)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        res = {}\n",
    "        for i in range(len(senders)):\n",
    "            if senders[i] not in res:\n",
    "                res[senders[i]] = len(messages[i].split())\n",
    "            else:\n",
    "                res[senders[i]] += len(messages[i].split())\n",
    "        return max(res,key=lambda k : (res[k],k))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        def count(s):\n",
    "            ret=0\n",
    "            ls=s.split(\" \")\n",
    "            for l in ls:\n",
    "                if l.strip()!=\"\":\n",
    "                    ret+=1\n",
    "            return ret\n",
    "        n=len(messages)\n",
    "        c=collections.Counter()\n",
    "        for i in range(n):\n",
    "            send,s=senders[i],messages[i]\n",
    "            c[send]+=count(messages[i])\n",
    "        m=-1\n",
    "        name=\"\"\n",
    "        for key in c:\n",
    "            if c[key]>m:\n",
    "                m=c[key]\n",
    "                name=key\n",
    "            elif c[key]==m:\n",
    "                name=max(name,key)\n",
    "        return name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        cnt = Counter()\n",
    "        for mess, sender in zip(messages, senders):\n",
    "            cnt[sender] += len(mess.split())\n",
    "        mx = max(cnt.values())\n",
    "        ans = []\n",
    "        for k, v in cnt.items():\n",
    "            if v == mx:\n",
    "                ans.append(k)\n",
    "        return sorted(ans)[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        name=Counter()\n",
    "        res=[]\n",
    "        maxL=-1\n",
    "        for i in range(len(messages)):\n",
    "            name[senders[i]]+=len(messages[i].split())\n",
    "            if name[senders[i]]>=maxL:\n",
    "                if name[senders[i]]>maxL:\n",
    "                    res=[senders[i]]\n",
    "                    maxL=name[senders[i]]\n",
    "                else:\n",
    "                    res.append(senders[i])\n",
    "        res.sort()\n",
    "        return res[-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        max_cnt = 0\n",
    "        res = ''\n",
    "        cnt = defaultdict(int)\n",
    "\n",
    "        for m,s in zip(messages,senders):\n",
    "            words = m.count(' ')+1\n",
    "            cnt[s] += words\n",
    "            if cnt[s] > max_cnt:\n",
    "                max_cnt = cnt[s]\n",
    "                res =  s \n",
    "            elif cnt[s] == max_cnt:\n",
    "                res = max(res,s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        m = {}\n",
    "        for i, message in enumerate(messages):\n",
    "            name = senders[i]\n",
    "            cnt = len(message.split(\" \"))\n",
    "            if name not in m:\n",
    "                m[name] = cnt\n",
    "            else:\n",
    "                m[name] += cnt\n",
    "        maxV = max(m.values())\n",
    "        res = []\n",
    "        for k, v in m.items():\n",
    "            if v == maxV:\n",
    "                res.append(k)\n",
    "        return sorted(res)[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        cnt = Counter()\n",
    "        for m,s in zip(messages,senders): cnt[s] += len(m.split())\n",
    "        max_cnt,ans = -1,''\n",
    "        for s,c in cnt.items():\n",
    "            if c > max_cnt: max_cnt,ans = c,s\n",
    "            elif c == max_cnt: ans = max(ans,s)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        word_count = {}\n",
    "        for i in range(len(messages)):\n",
    "            sender = senders[i]\n",
    "            message = messages[i]\n",
    "            words = message.split()\n",
    "            if sender not in word_count:\n",
    "                word_count[sender] = len(words)\n",
    "            else:\n",
    "                word_count[sender] += len(words)\n",
    "        \n",
    "        max_word_count = 0\n",
    "        max_sender = \"\"\n",
    "        for sender, count in word_count.items():\n",
    "            if count > max_word_count:\n",
    "                max_word_count = count\n",
    "                max_sender = sender\n",
    "            elif count == max_word_count:\n",
    "                if sender > max_sender:\n",
    "                    max_sender = sender\n",
    "        \n",
    "        return max_sender"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        n = len(messages)\n",
    "        d = {}\n",
    "        for i in range(n):\n",
    "            wc = messages[i].count(' ') + 1\n",
    "            sender = senders[i]\n",
    "            d[sender] = wc if sender not in d else d[sender] + wc\n",
    "        res = ''\n",
    "        count = 0\n",
    "        for sender, wc in d.items():\n",
    "            if wc > count:\n",
    "                res = sender\n",
    "                count = wc\n",
    "            elif wc == count and sender > res:\n",
    "                res = sender\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        res=\"\"\n",
    "        cnt=Counter()\n",
    "        for a,b in zip(messages,senders):\n",
    "            cnt[b]+=len(a.split())\n",
    "        \n",
    "\n",
    "        mx=max(cnt.values())\n",
    "\n",
    "        for a in senders:\n",
    "            if cnt[a]==mx:\n",
    "                if not res or res<a:\n",
    "                    res=a\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dic={}\n",
    "        for i in range(0,len(messages)):\n",
    "            dic[senders[i]]=dic.get(senders[i],0)+len(messages[i].split())\n",
    "        maxpeople=senders[0]\n",
    "        maxnums=dic[maxpeople]\n",
    "        for i in dic.keys():\n",
    "            if dic[i]>maxnums or dic[i]==maxnums and i>maxpeople:\n",
    "                maxpeople=i\n",
    "                maxnums=dic[i]\n",
    "        return maxpeople\n",
    "        \n",
    "\n",
    "            \n",
    "            \n",
    "        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        d = defaultdict(int)\n",
    "        max_val = 0\n",
    "        max_idx = \"\"\n",
    "        for m, s in zip(messages, senders):\n",
    "            d[s] += len(m.split(\" \"))\n",
    "            if d[s] > max_val or (d[s] == max_val and s > max_idx):\n",
    "                max_idx = s\n",
    "                max_val = d[s]\n",
    "        return max_idx\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution(object):\n",
    "    def largestWordCount(self, messages, senders):\n",
    "        \"\"\"\n",
    "        :type messages: List[str]\n",
    "        :type senders: List[str]\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        dict_ = {}\n",
    "        maxcount, maxsender = 0, \"\"\n",
    "        for i, sender in enumerate(senders):\n",
    "            if sender in dict_:\n",
    "                dict_[sender] += messages[i].count(' ') + 1\n",
    "            else:\n",
    "                dict_[sender] = messages[i].count(' ') + 1\n",
    "            if dict_[sender] > maxcount:\n",
    "                maxcount, maxsender = dict_[sender], sender\n",
    "            elif dict_[sender] == maxcount:\n",
    "                maxsender = max(maxsender, sender)\n",
    "        return maxsender\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: list[str], senders: list[str]) -> str:\n",
    "        res = defaultdict(int)\n",
    "        for i,j in enumerate(messages):\n",
    "            cnt = 1\n",
    "            for n in j:\n",
    "                if n == ' ':\n",
    "                    cnt += 1\n",
    "            res[senders[i]] += cnt\n",
    "        value = max(res.values())\n",
    "        ans = []\n",
    "        for i in res:\n",
    "            if res[i] == value:\n",
    "                ans.append(i)\n",
    "        x = sorted(ans,reverse=True)\n",
    "        return x[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        cnt = 0  # max_cnt\n",
    "        person = \"\"\n",
    "        c = Counter()\n",
    "        for msg, sender in zip(messages, senders):\n",
    "            c[sender] += len(msg.split())\n",
    "            if c[sender] >= cnt:\n",
    "                person = max(person, sender) if cnt == c[sender] else sender\n",
    "                cnt = c[sender]\n",
    "        return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        d = collections.defaultdict(int)\n",
    "        for i,e in enumerate(senders):\n",
    "            d[e]+=len(messages[i].split(' '))\n",
    "        m,pre = 0,''\n",
    "        for k in d:\n",
    "            if d[k]>m or (d[k]==m and k>pre):\n",
    "                m=d[k]\n",
    "                pre = k\n",
    "        return pre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        win={}\n",
    "        n = len(messages)\n",
    "        ans=\"\"\n",
    "        max_token=0\n",
    "        for i in range(n):\n",
    "            win[senders[i]]=win.get(senders[i],0)+len(messages[i].split())\n",
    "            if win[senders[i]]>max_token:\n",
    "                max_token=win[senders[i]]\n",
    "                ans = senders[i]\n",
    "            elif win[senders[i]]==max_token:\n",
    "                ans = max(senders[i],ans)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dict1 = collections.defaultdict(int)\n",
    "        for i,content in enumerate(messages):\n",
    "            dict1[senders[i]] += len(content.split(' '))\n",
    "        tmp = []\n",
    "        maxnum = max(dict1.values())\n",
    "        for p,q in dict1.items():\n",
    "            if q == maxnum:\n",
    "                tmp.append(p)\n",
    "        tmp.sort(reverse=True)\n",
    "        return tmp[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "\n",
    "        dic = {}\n",
    "        for name in senders:\n",
    "            dic[name] = 0\n",
    "\n",
    "        for i, name in enumerate(senders):\n",
    "            dic[name] += messages[i].count(' ') + 1\n",
    "        res = 0\n",
    "        max_name = \"\"\n",
    "        for s in dic:\n",
    "\n",
    "            if res < dic[s] or (res == dic[s] and max_name < s):\n",
    "                res = dic[s]\n",
    "                max_name = s\n",
    "        return max_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        cnts = {}\n",
    "        res = senders[0]\n",
    "\n",
    "        for i in range(len(messages)):\n",
    "            message = messages[i]\n",
    "            cnts[senders[i]] = cnts.get(senders[i], 0) + len(message.split(\" \"))\n",
    "            if cnts[senders[i]] > cnts.get(res):\n",
    "                res = senders[i]\n",
    "            elif cnts[senders[i]] == cnts.get(res):\n",
    "                if senders[i] > res:\n",
    "                    res = senders[i]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        aaa=Counter()\n",
    "        for i,x in enumerate(messages):\n",
    "            aaa[senders[i]] += len(x.split())\n",
    "        maxval=max(aaa.values())\n",
    "        max_key = [key for key, value in aaa.items() if value == maxval]\n",
    "        return max(max_key)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "      data = {}\n",
    "      for i in range(len(messages)):\n",
    "        if senders[i] not in data.keys():\n",
    "          data[senders[i]] = len(messages[i].split(' '))\n",
    "        else:\n",
    "          data[senders[i]] += len(messages[i].split(' '))\n",
    "\n",
    "      max_value = max(data.values())\n",
    "      return max([k for k,v in data.items() if v==max_value])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dect = defaultdict(int)\n",
    "        res = \"\"\n",
    "        max_len = 0\n",
    "        for i in range(len(senders)):\n",
    "            dect[senders[i]] += len(messages[i].split(\" \"))\n",
    "            if dect[senders[i]] > max_len:\n",
    "                max_len = max(max_len,dect[senders[i]])\n",
    "                res = senders[i]\n",
    "            elif dect[senders[i]] == max_len:\n",
    "                res = max(res,senders[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        res = {}\n",
    "        for i in range(len(senders)):\n",
    "            if senders[i] not in res:\n",
    "                res[senders[i]] = len(messages[i].split())\n",
    "            else:\n",
    "                res[senders[i]] += len(messages[i].split())\n",
    "        return max(res,key=lambda k : (res[k],k))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        res = {}\n",
    "        max_index = 0\n",
    "        for i in range(len(senders)):\n",
    "            if senders[i] not in res:\n",
    "                res[senders[i]] = len(messages[i].split())\n",
    "            else:\n",
    "                res[senders[i]] += len(messages[i].split())\n",
    "            if res[senders[i]] > res[senders[max_index]] or (res[senders[i]] == res[senders[max_index]] and senders[i] > senders[max_index]):\n",
    "                max_index = i\n",
    "        return senders[max_index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        d = {}\n",
    "        for i in range(len(messages)):\n",
    "            sender = senders[i]\n",
    "            if sender in d:\n",
    "                d[sender] += len(messages[i].split())\n",
    "            else:\n",
    "                d[sender] = len(messages[i].split())\n",
    "        res = ''\n",
    "        mmax = 0\n",
    "        for k in d:\n",
    "            if d[k] > mmax or (d[k] == mmax and k > res):\n",
    "                mmax = d[k]\n",
    "                res = k\n",
    "        return res                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        s = [0,'']\n",
    "        dic = collections.defaultdict(int)\n",
    "        for i in range(len(senders)):\n",
    "            dic[senders[i]] += len(messages[i].split(' '))\n",
    "            if dic[senders[i]]>s[0] or (dic[senders[i]]==s[0] and senders[i]>s[1]):\n",
    "                s = [dic[senders[i]],senders[i]]\n",
    "        return s[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from operator import gt\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: list[str], senders: list[str]) -> str:\n",
    "        ret: dict[str, int] = {}\n",
    "        mx = 0\n",
    "        name = \"\"\n",
    "        for i in range(0, len(messages)):\n",
    "            s = senders[i]\n",
    "            ret[s] = ret.get(s, 0) + messages[i].count(\" \") + 1\n",
    "            if ret[s] > mx or (ret[s] == mx and gt(s, name)):\n",
    "                mx = ret[s]\n",
    "                name = s\n",
    "        return name\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from operator import gt\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: list[str], senders: list[str]) -> str:\n",
    "        ret: dict[str, int] = {}\n",
    "        mx = 0\n",
    "        name = \"\"\n",
    "        for i in range(0, len(messages)):\n",
    "            s = senders[i]\n",
    "            ret[s] = ret.get(s, 0) + len(messages[i].split(\" \"))\n",
    "            if ret[s] > mx or (ret[s] == mx and gt(s, name)):\n",
    "                mx = ret[s]\n",
    "                name = s\n",
    "        return name\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        wordsBySender = defaultdict(int)\n",
    "        for m, s in zip(messages, senders):\n",
    "            wordsBySender[s] += m.count(' ') + 1\n",
    "        return max((n, s) for s, n in wordsBySender.items())[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        R = Counter()\n",
    "        for s,u in zip(messages,senders):\n",
    "            R[u] += len(s.split())\n",
    "        res = ''\n",
    "        cnt = -1\n",
    "        for u,v in R.items():\n",
    "            if v > cnt or v == cnt and u > res:\n",
    "                res = u\n",
    "                cnt = v\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from operator import gt\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: list[str], senders: list[str]) -> str:\n",
    "        ret: dict[str, int] = {}\n",
    "        mx = 0\n",
    "        name = \"\"\n",
    "        for i in range(0, len(messages)):\n",
    "            s = senders[i]\n",
    "            ret[s] = ret.get(s, 0) + len(messages[i].split(\" \"))\n",
    "            if ret[s] > mx or (ret[s] == mx and gt(s, name)):\n",
    "                mx = ret[s]\n",
    "                name = s\n",
    "        return name\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dic={}\n",
    "        for i in range(0,len(messages)):\n",
    "            dic[senders[i]]=dic.get(senders[i],0)+len(messages[i].split())\n",
    "        maxpeople=senders[0]\n",
    "        maxnums=dic[maxpeople]\n",
    "        for i in dic.keys():\n",
    "            if dic[i]>maxnums or dic[i]==maxnums and i>maxpeople:\n",
    "                maxpeople=i\n",
    "                maxnums=dic[i]\n",
    "        return maxpeople\n",
    "        \n",
    "\n",
    "            \n",
    "            \n",
    "        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dic = {}\n",
    "        n = len(messages)\n",
    "        for i in range(n):\n",
    "            arr = list(messages[i].split())\n",
    "            l = len(arr)\n",
    "            if senders[i] in dic:\n",
    "                dic[senders[i]] += l\n",
    "            else:\n",
    "                dic.update({senders[i]: l})\n",
    "        a = list(dic.items())\n",
    "        a.sort(key = lambda x:[x[1], x[0]] )\n",
    "        return a[-1][0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "\n",
    "        dic=collections.Counter()\n",
    "\n",
    "        for i in range(len(messages)):\n",
    "            y=messages[i].split(\" \")\n",
    "            dic[senders[i]]+=len(y)\n",
    "\n",
    "        x=list(dic.items())\n",
    "\n",
    "        x.sort(key=lambda x:[x[1],x[0]])\n",
    "\n",
    "        return x[-1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        n = len(messages)\n",
    "        d = dict()\n",
    "        ret = 0\n",
    "        for i in range(n):\n",
    "            s = senders[i]\n",
    "            m = messages[i].count(' ') + 1\n",
    "            d[s] = k = d.get(s, 0) + m\n",
    "            if k > ret or (k == ret and s > rets):\n",
    "                ret, rets = k, s\n",
    "        return rets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        rec = defaultdict(int)\n",
    "        for msg,name in zip(messages, senders):\n",
    "            rec[name] += len(msg.split())\n",
    "        max_len = max([v for k,v in rec.items()])\n",
    "        name_list = []\n",
    "        for k,v in rec.items():\n",
    "            if v==max_len:\n",
    "                name_list.append(k)\n",
    "        name_list.sort()\n",
    "        return name_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        hashmap = {}\n",
    "        for message, sender in zip(messages, senders):\n",
    "            length = len(message.split(\" \"))\n",
    "            try:\n",
    "                hashmap[sender] += length\n",
    "            except:\n",
    "                hashmap[sender] = length\n",
    "        max_val = max(hashmap.values())\n",
    "        ans = []\n",
    "        for k, v in hashmap.items():\n",
    "            if v == max_val:\n",
    "                ans.append(k)\n",
    "        ans.sort()\n",
    "\n",
    "        print(hashmap)\n",
    "        print(max_val)\n",
    "        return ans[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        cnt = defaultdict(int)\n",
    "        for message, sender in zip(messages, senders): \n",
    "            cnt[sender] += len(message.split())\n",
    "        return list(sorted(sorted(cnt.items(), reverse=True), key=lambda item: item[1], reverse=True))[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dic = {}\n",
    "        for i, name in enumerate(senders):\n",
    "            dic[name] = dic.get(name, 0) + messages[i].count(' ') + 1\n",
    "\n",
    "        ans_name = senders[0]\n",
    "        max_c = cnt = 0\n",
    "        for k, v in dic.items():\n",
    "            if v > max_c:\n",
    "                max_c = v\n",
    "                ans_name = k \n",
    "            if v == max_c and k > ans_name:\n",
    "                ans_name = k\n",
    "\n",
    "        print(dic)\n",
    "        # l1.sort(key=lambda x: (x[1], x[0]), reverse=True)\n",
    "        return ans_name\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dict = {a:0 for a in senders}\n",
    "        # for sender in senders:\n",
    "        #     dict[sender] = 0\n",
    "        n = len(messages)\n",
    "        for i in range(n):\n",
    "            dict[senders[i]] += len(messages[i].split(\" \"))\n",
    "        res = sorted(dict.items(), key=lambda k: (k[1], k[0]))\n",
    "      \n",
    "        return res[-1][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        count={}\n",
    "\n",
    "        for sender, message in zip(senders, messages):\n",
    "            length = len(message.split())\n",
    "            count[sender] = count.get(sender, 0) + length\n",
    "\n",
    "        return sorted(list(count.keys()), key=lambda x:(count[x], x), reverse=True)[0]\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        mess_dict = dict()\n",
    "        for i, x in enumerate(senders):\n",
    "            if x in mess_dict:\n",
    "                mess_dict[x] += messages[i].count(' ') + 1\n",
    "            else:\n",
    "                mess_dict.update({x:messages[i].count(' ') + 1})\n",
    "        new_list = sorted(mess_dict.items(), key=lambda x: (x[1], x[0]), reverse=True)\n",
    "\n",
    "        return new_list[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        mess_dict = dict()\n",
    "        for i, x in enumerate(senders):\n",
    "            if x in mess_dict:\n",
    "                mess_dict[x] += len(messages[i].split(' '))\n",
    "            else:\n",
    "                mess_dict.update({x:len(messages[i].split(' '))})\n",
    "        new_list = sorted(mess_dict.items(), key=lambda x: (x[1], x[0]), reverse=True)\n",
    "\n",
    "        return new_list[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        cnt=defaultdict(int)\n",
    "        for message,sender in zip(messages,senders):\n",
    "            cnt[sender]+=len(message.split(' '))\n",
    "        return sorted(cnt.items(),key=lambda x:(x[1],x[0]),reverse=True)[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dic={a:0 for a in senders}\n",
    "        for i in range(len(messages)):\n",
    "            dic[senders[i]]+=len(messages[i].split(' '))\n",
    "        # res=[]\n",
    "        # for key,val in dic.items():\n",
    "        #     res.append([key,val])\n",
    "        # print(res)\n",
    "        # res.sort(key=lambda x:(-x[1],-ord(x[0][0]) ),reverse=False)\n",
    "        # return res[0][0]\n",
    "        # #return max(dic.items(),key=lambda x:x[1])[0]\n",
    "        return sorted(dic.items(), key=lambda x: (x[1], x[0]))[-1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        # 初始化一个字典用于存储每个发件人及其发送的单词数\n",
    "        sender_dict = {}\n",
    "        \n",
    "        # 将每个发件人的单词计数初始化为0\n",
    "        for sender in senders:\n",
    "            sender_dict[sender] = 0\n",
    "        \n",
    "        # 计算每个发件人发送的单词数\n",
    "        length = len(messages)\n",
    "        for i in range(length):\n",
    "            sender_dict[senders[i]] += len(messages[i].split(\" \"))\n",
    "        \n",
    "        # 按单词数量和字典序对发件人进行排序\n",
    "        words_cnt = sorted(sender_dict.items(), key=lambda kv: (kv[1], kv[0]))\n",
    "        \n",
    "        # 返回单词数最多且字典序最大的发件人\n",
    "        return words_cnt[-1][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter, defaultdict\n",
    "from functools import reduce\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages, senders) -> str:\n",
    "        senders_dict = defaultdict(int)\n",
    "        for i, message in enumerate(messages):\n",
    "            senders_dict[senders[i]] += len(message.split(\" \"))\n",
    "        return sorted(senders_dict.items(), key=lambda x:(x[1], x[0]), reverse=True)[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        d = {}\n",
    "        for sender in senders:\n",
    "            d[sender] = 0\n",
    "        length = len(messages)\n",
    "        for i in range(length):\n",
    "            d[senders[i]] += len(messages[i].split(\" \"))\n",
    "        words_cnt = sorted(d.items(), key = lambda x:(x[1], x[0]))\n",
    "        return words_cnt[-1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dict = {}\n",
    "        for sender in senders:\n",
    "            dict[sender] = 0\n",
    "        n = len(messages)\n",
    "        for i in range(n):\n",
    "            dict[senders[i]] += len(messages[i].split(\" \"))\n",
    "        res = sorted(dict.items(), key=lambda k: (k[1], k[0]))\n",
    "        return res[-1][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        send = Counter()\n",
    "        \n",
    "        for message , sender in zip(messages , senders):\n",
    "            send[sender] += len(message.split())\n",
    "\n",
    "        return sorted(send.items() , key = lambda x :(x[1] , x[0]))[-1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dict = {}\n",
    "        for sender in senders:\n",
    "            dict[sender] = 0\n",
    "        n = len(messages)\n",
    "        for i in range(n):\n",
    "            dict[senders[i]] += len(messages[i].split(\" \"))\n",
    "        res = sorted(dict.items(), key=lambda k: (k[1], k[0]))\n",
    "      \n",
    "        return res[-1][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dic={a:0 for a in senders}\n",
    "        for i in range(len(messages)):\n",
    "            dic[senders[i]]+=len(messages[i].split(' '))\n",
    "        # res=[]\n",
    "        # for key,val in dic.items():\n",
    "        #     res.append([key,val])\n",
    "        # print(res)\n",
    "        # res.sort(key=lambda x:(-x[1],-ord(x[0][0]) ),reverse=False)\n",
    "        # return res[0][0]\n",
    "        # #return max(dic.items(),key=lambda x:x[1])[0]\n",
    "        return sorted(dic.items(), key=lambda x: (x[1], x[0]))[-1][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        d = collections.defaultdict(int)\n",
    "        for i,e in enumerate(senders):\n",
    "            d[e]+=len(messages[i].split(' '))\n",
    "        d = list(d.items())\n",
    "        d.sort(key=lambda x:(x[1],x[0]),reverse=True)\n",
    "        return d[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages, senders) -> str:\n",
    "        senders_dict = defaultdict(int)\n",
    "        for i, message in enumerate(messages):\n",
    "            senders_dict[senders[i]] += len(message.split(\" \"))\n",
    "        return sorted(senders_dict.items(), key=lambda x:(x[1], x[0]), reverse=True)[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages, senders):\n",
    "        dic={}\n",
    "        for i,name in enumerate(senders):\n",
    "            if name not in dic:\n",
    "                dic[name]=0\n",
    "            dic[name] +=len(messages[i].split())\n",
    "        print(dic)\n",
    "        maxname=senders[0]\n",
    "        maxnum=0\n",
    "        for name,num in dic.items():\n",
    "            print(name,num)\n",
    "            if num>maxnum:\n",
    "                maxname=name\n",
    "                maxnum = num\n",
    "            elif num==maxnum:\n",
    "                maxname=max(maxname,name)\n",
    "                maxnum = num\n",
    "\n",
    "        return maxname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        dic = {}\n",
    "        n = len(messages)\n",
    "        for i in range(n):\n",
    "            arr = list(messages[i].split())\n",
    "            l = len(arr)\n",
    "            if senders[i] in dic:\n",
    "                dic[senders[i]] += l\n",
    "            else:\n",
    "                dic.update({senders[i]: l})\n",
    "        a = list(dic.items())\n",
    "        print(a)\n",
    "        a.sort(key = lambda x:[x[1], x[0]] )\n",
    "        return a[-1][0]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        tmp = {}\n",
    "        for i in range(len(senders)):\n",
    "            if senders[i] not in tmp:\n",
    "                tmp[senders[i]] = len(messages[i].split(' '))\n",
    "            else:\n",
    "                tmp[senders[i]] += len(messages[i].split(' '))\n",
    "        ans = []\n",
    "        for k, v in tmp.items():\n",
    "            ans.append([k, v])\n",
    "        ans.sort(key = lambda x: (x[1], x[0]), reverse = True)\n",
    "        return ans[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        Names = set(senders)\n",
    "        Cnt = dict()\n",
    "        for i, sd in enumerate(senders):\n",
    "            Cnt[sd] = Cnt.get(sd, 0) + messages[i].count(' ') + 1\n",
    "        ans = None\n",
    "        Cnt[None] = -inf\n",
    "        for name in Names:\n",
    "            print(name)\n",
    "            if Cnt[ans] < Cnt[name] or (Cnt[ans] == Cnt[name] and ans < name):\n",
    "                ans = name\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        hashmap = {}\n",
    "        for message, sender in zip(messages, senders):\n",
    "            length = len(message.split(\" \"))\n",
    "            try:\n",
    "                hashmap[sender] += length\n",
    "            except:\n",
    "                hashmap[sender] = length\n",
    "        return max([(v, k) for k, v in hashmap.items()])[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        counter = defaultdict(int)\n",
    "        for mes, sender in zip(messages, senders):\n",
    "            counter[sender] += len(mes.split())\n",
    "        return max([(v, k) for k, v in counter.items()])[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages, senders):\n",
    "        dic={}\n",
    "        for i,name in enumerate(senders):\n",
    "            if name not in dic:\n",
    "                dic[name]=0\n",
    "            dic[name] +=len(messages[i].split())\n",
    "        print(dic)\n",
    "        maxname=senders[0]\n",
    "        maxnum=0\n",
    "        for name,num in dic.items():\n",
    "            print(name,num)\n",
    "            if num>maxnum:\n",
    "                maxname=name\n",
    "                maxnum = num\n",
    "            elif num==maxnum:\n",
    "                maxname=max(maxname,name)\n",
    "            else:\n",
    "                continue\n",
    "            maxnum = num\n",
    "\n",
    "        return maxname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        c = Counter()\n",
    "        for m, s in zip(messages, senders):\n",
    "            c[s]+=len(m.split())\n",
    "        t = [(val, key) for key,val in c.items()]\n",
    "        return sorted(t, reverse=True)[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        d = {}\n",
    "        n = len(messages)\n",
    "        for i in range(n):\n",
    "            k = len(messages[i].split(' '))\n",
    "            if senders[i] in d:\n",
    "                d[senders[i]] += k\n",
    "            else:\n",
    "                d[senders[i]] = k\n",
    "        #print(d)\n",
    "        res = []\n",
    "        for i in d:\n",
    "            res.append([d[i], i])\n",
    "        res.sort(reverse=True)\n",
    "        return res[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestWordCount(self, messages: List[str], senders: List[str]) -> str:\n",
    "        send = Counter()\n",
    "        \n",
    "        for message , sender in zip(messages , senders):\n",
    "            send[sender] += len(message.split())\n",
    "        print(send)\n",
    "        return sorted(send.items() , key = lambda x :(x[1] , x[0]))[-1][0]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
